import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author Mucd
 * @ClassName Add
 * @Date 2020/12/31/031
 * @Specification Alibaba
 */
public class ManagementSystem {
    static ArrayList<Student> arrayList = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);
    /**
     * 添加学生信息
     * ID不能重复
     * true为重复 false不重复
     */
    public static boolean isUsed(String sid) {
        boolean flag = false;
        for (Student s : arrayList) {
            //新增ID和已有ID做对比,true为重复 false不重复
            if (s.getSid().equals(sid)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 程序入口
     * @throws IOException
     */
    public void information() throws IOException {
        while (true) {
            System.out.println("**********");
            System.out.println("19APP4 Mucd ");
            System.out.println("管  理  系  统");
            System.out.println("**********");
            System.out.println("1.添加学生信息 2.删除学生信息 3.修改学生信息 4.查询所有学生信息 5.查詢指定ID信息 0.退出");
            Scanner sc = new Scanner(System.in);
            int scInt = sc.nextInt();
            switch (scInt) {
                case 1:
                    add();
                    break;
                case 2:
                    removeStudent();
                    break;
                case 3:
                    setStudent();
                    break;
                case 4:
                    searchAllStudent();
                    break;
                case 5:
                    query();
                    break;
                case 0:
                    System.out.println("正在退出 ... ");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入数字使用功能");
            }
        }
    }

    public void add() {
        Scanner sc = new Scanner(System.in);
        //创建学生对象,将数据数据赋值给成员变量
        Student student = new Student("N", 19);
        //输入信息
        System.out.println("输入姓名");
        String name = sc.next();
        System.out.println("输入年龄");
        int age = sc.nextInt();
        System.out.println("输入性别");
        String sex = sc.next();
        //ID不能重复
        while (true) {
            System.out.println("输入ID");
            String sid = sc.next();
            boolean flag = isUsed(sid);
            if (flag) {
                System.out.println("ID已使用");
            } else {
                student.setSid(sid);
                arrayList.add(student);
                break;
            }
        }
        //经数据设置进去
        student.setAge(age);
        student.setName(name);
        student.setSex(sex);
        System.out.println("添加完成");
    }

    /**
     * 查询指定学生信息
     */
    public void query(){
        System.out.println("输入想要查询的学生的ID");
        if (arrayList.size() == 0){
            System.out.println("沒有數據，請先添加");
            return;
        }
        String scId = sc.next();
        for (Student s : arrayList) {
            if (scId.equals(s.getSid())) {
                System.out.println("姓名: " + s.getName() + "  " + "性别: " + s.getSex() + "  " + "年龄: " + s.getAge() + "  " + "ID: " + s.getSid() + "\n");
            }
        }
    }

    /**
     * 查询所有学生信息
     */
    public void searchAllStudent() throws IOException {
        //先判断是不是空数据
        if (arrayList.size() == 0) {
            System.out.println("数据为空,先添加数据");
            return;
        }
        //D盘根目录生成 2.txt文件
        FileWriter writer = new FileWriter("D:\\2.txt");
        //将数据按格式输出 写入到文件
        for (Student s : arrayList) {
            System.out.println("姓名: " + s.getName() + "  " + "性别: " + s.getSex() + "  " + "年龄: " + s.getAge() + "  " + "ID: " + s.getSid() + "\n");
            writer.write("姓名: " + s.getName() + "  " + "性别: " + s.getSex() + "  " + "年龄: " + s.getAge() + "  " + "ID: " + s.getSid() + "\n");
            writer.flush();
        }
        System.out.println();
    }

    /**
     * 修改学生信息
     */
    public void setStudent() {
        //没有此数据
        if (arrayList.size() == 0) {
            System.out.println("没有数据,请先添加");
            add();
            return;
        }

        System.out.println("输入要修改的学生ID");
        String idModify = sc.next();
        for (int i = 0; i < arrayList.size(); i++) {
            Student stu = arrayList.get(i);
            //输入错误ID号
            if (!idModify.equals(stu.getSid())) {
                System.out.println("没有此ID,重新选择功能");
                break;
            }
            //有ID号
            if (idModify.equals(stu.getSid())) {
                System.out.println("姓名修改为");
                String name = sc.next();
                System.out.println("年龄修改为");
                int age = sc.nextInt();
                System.out.println("性别修改为");
                String sex = sc.next();
                //将修改的数据保存
                Student student = new Student("N", 19);
                student.setAge(age);
                student.setName(name);
                student.setSex(sex);
                student.setSid(idModify);
                arrayList.set(i, student);
                System.out.println("修改完成✔✔✔✔✔✔");
                break;
            }
        }
    }

    /**
     * 删除学生信息
     */
    public void removeStudent() {
        System.out.println("输入要删除的学生ID");
        String removeId = sc.next();
        //遍历出信息
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (removeId.equals(student.getSid())) {
                System.out.println("删除完成" + arrayList.remove(i));
            }
        }
    }
}